package com.api.volunteer.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.volunteer.annoation.NoAuth;
import com.api.volunteer.dto.UserDTO;
import com.api.volunteer.model.*;
import com.api.volunteer.repository.AccountRepository;
import com.api.volunteer.repository.LoginTokenRepository;
import com.api.volunteer.ro.UserCode;
import com.api.volunteer.service.LoginTokenService;
import com.api.volunteer.util.Holder;
import com.api.volunteer.util.Result;
import com.api.volunteer.util.WXUtils;
import com.api.volunteer.vo.UserDataVo;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author 邹宇杰
 * @email 2901570623@qq.com
 * @date 2022/3/29 21:58
 */
@RestController
public class LoginController extends BaseController{

    private final String url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";

    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.secret}")
    private String secret;

    @Autowired
    private LoginTokenService loginTokenService;

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    Holder holder;

    /**
     * 用户code获取用户的唯一标识信息
     * @param code 用户code
     * @return 小程序token 用于验证请求
     */
    @GetMapping("/getCode/{code}")
    @NoAuth
    public Result getCode(@PathVariable("code") String code){
        String format = String.format(url, appid, secret, code);
        RestTemplate restTemplate = new RestTemplate();
        String str = restTemplate.getForObject(format, String.class);
        UserCode userCode = JSONObject.parseObject(str, UserCode.class);
        return this.processData(()->{return loginTokenService.getToken(userCode);});
    }

    /**
     * 验证token用户信息状态
     * @return result
     */
    @GetMapping("/validateToken")
    public Result validateToken(){

        Result result = new Result();

        UserEntity user = holder.getUser();

        if(Objects.equals(user.getSecondSubmit(), Status.Seccess) && Objects.equals(user.getThirdSubmit(), Status.Seccess) && Objects.equals(user.getAccountSource(), AccountSource.VOLUNTEER)){
            return result.put("code", 203).put("message", "所有信息采集过了");
        }
        if(Objects.equals(user.getSecondSubmit(), Status.Seccess) && Objects.equals(user.getThirdSubmit(), Status.Seccess) && Objects.equals(user.getAccountSource(), AccountSource.EMPLOYER)){

            Account account = accountRepository.findAccountByOpenid(user.getOpenid());

            if(Objects.equals(account.getStatus(), Status.Auditing)){
                return result.put("code", 209).put("message", "当前雇主所有信息采集完毕, 正在审核中");
            }else if (Objects.equals(account.getStatus(), Status.AuditSuccess)) {
                return result.put("code", 211).put("message", "当前雇主所有信息采集完毕, 审核通过");
            }else{
                return result.put("code", 212).put("message", "当前雇主所有信息采集完毕, 审核失败, 请重新申请表单");
            }

        }
        if(!Objects.equals(user.getSecondSubmit(), Status.Seccess) ){
            return result.put("code", 205).put("message", "第二次信息没有采集");
        }
        if(!Objects.equals(user.getThirdSubmit(), Status.Seccess) && Objects.equals(user.getAccountSource(), AccountSource.VOLUNTEER) ){
            return result.put("code", 206).put("message", "志愿者第三次信息没有采集");
        }
        if(!Objects.equals(user.getThirdSubmit(), Status.Seccess) && Objects.equals(user.getAccountSource(), AccountSource.EMPLOYER) ){
            return result.put("code", 208).put("message", "雇主第三次信息没有采集");
        }
        return result.put("code", 207).put("message", "该用户没有填写任何信息");
    }

    /**
     * 处理加密数据
     * @param userDataVo 前端传输的数据
     * @return 返回请求状态
     */
    @PostMapping("/parseData")
    public Result authorization(@RequestBody UserDataVo userDataVo){
        return loginTokenService.processData(userDataVo);
    }
}